package com.jet.auth.service;

import cn.hutool.core.util.StrUtil;
import com.jet.auth.domain.UserDetail;
import com.jet.core.enums.CommonDeleted;
import com.jet.core.exception.CustomException;
import com.jet.merchant.domain.entity.*;
import com.jet.merchant.domain.vo.MerchantStoreVo;
import com.jet.merchant.domain.vo.ResourceVo;
import com.jet.merchant.service.*;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserDetailsServiceImpl {

    private final IMerchantService merchantService;
    private final IMerchantStoreService merchantStoreService;
    private final IClerkAccountRoleMapService clerkAccountRoleMapService;
    private final IClerkAccountService clerkAccountService;
    private final IClerkResourceService clerkResourceService;

    public UserDetailsServiceImpl(IMerchantService merchantService, IMerchantStoreService merchantStoreService, IClerkAccountRoleMapService clerkAccountRoleMapService, IClerkAccountService clerkAccountService, IClerkResourceService clerkResourceService) {
        this.merchantService = merchantService;
        this.merchantStoreService = merchantStoreService;
        this.clerkAccountRoleMapService = clerkAccountRoleMapService;
        this.clerkAccountService = clerkAccountService;
        this.clerkResourceService = clerkResourceService;
    }


    /**
     * 根据手机号加载用户信息
     *
     * @param phone 手机号
     * @return 用户信息
     */
    public UserDetail loadUserByPhone(String phone) {
        ClerkAccount entity = clerkAccountService.findByPhone(phone);
        return convertUser(entity);
    }


    public UserDetail loadUserById(String id) {
        ClerkAccount entity = clerkAccountService.getById(id);
        return convertUser(entity);
    }

    private UserDetail convertUser(ClerkAccount entity) {
        if (entity == null || entity.getDeleted() == CommonDeleted.DELETED.getCode()) {
            throw new CustomException("账号错误");
        }
        ClerkAccountRoleMap userRole = clerkAccountRoleMapService.findByAccountId(entity.getId());
        if (userRole == null) {
            throw new CustomException("账号错误");
        }
        List<ResourceVo> resourceVos = clerkResourceService.findAllByRoleId(userRole.getRoleId());
        List<String> perms = resourceVos.stream()
                .map(ResourceVo::getPermissions)
                .filter(StrUtil::isNotBlank)
                .distinct().collect(Collectors.toList());

        Merchant merchant = merchantService.findByMerchantNo(entity.getMerchantNo());
        if (merchant == null) {
            throw new CustomException("商户信息错误");
        }

        MerchantStoreVo store = merchantStoreService.findByStoreNo(entity.getMerchantNo(), entity.getStoreNo());
        if (store == null) {
            throw new CustomException("门店信息错误");
        }

        UserDetail userDetail = new UserDetail();
        userDetail.setId(entity.getId());
        userDetail.setMerchantNo(entity.getMerchantNo());
        userDetail.setStoreNo(entity.getStoreNo());
        userDetail.setUsername(entity.getPhone());
        userDetail.setPhone(entity.getPhone());
        userDetail.setPassword(entity.getPassword());
        userDetail.setStatus(entity.getStatus());
        userDetail.setMerchantStatus(merchant.getStatus());
        userDetail.setStoreStatus(store.getStatus());
        userDetail.setStoreExpireDate(store.getEndDate());
        userDetail.setPerms(perms);
        return userDetail;
    }
}
